/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import models.LiveLesson;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Vuong Pham<Vuong.Pham@hva.nl> 
 * Achie
 */
public class LiveLessonDao {
    
    private final Session session;

    public LiveLessonDao(Session session) {
        this.session = session;
    }

    public List<LiveLesson> findAll() {
        Transaction tx = session.beginTransaction();
        List<LiveLesson> list = session.createQuery("from LiveLesson as c").list();
        tx.commit();
        return list;
    }
    
    //get live lesson object from lesson id
     public LiveLesson getLiveLessonFromLessonId(long liveLesson) {
        Transaction tx = session.beginTransaction();
        List<LiveLesson> list = session.createQuery("from LiveLesson where lesson_lesson_id= :id").setParameter("id", liveLesson).setMaxResults(3).list();
        tx.commit();
        LiveLesson listx = list.get(0);
        return listx;
    }
   

    public boolean save(LiveLesson liveLesson) {
        try {
            Transaction tx = session.beginTransaction();
            session.save(liveLesson);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean update(LiveLesson liveLesson) {
        try {
            Transaction tx = session.beginTransaction();
            session.update(liveLesson);
            tx.commit();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public LiveLesson get(long id) {
        return (LiveLesson) session.get(LiveLesson.class, id);
    }
    

    public void delete(LiveLesson liveLesson) {
        Transaction tx = session.beginTransaction();
        session.delete(liveLesson);
        tx.commit();
    }
}
